package com.motu.monstercity.server.game.logic;

import com.motu.monstercity.protocol.Common;
import com.motu.monstercity.protocol.Common.PBUserBuddy;
import com.motu.monstercity.protocol.Common.PBUserData;
import com.motu.monstercity.protocol.CsGameSystem;
import com.motu.monstercity.protocol.CsGameSystem.CSGameSystemRedPointResponse;
import com.motu.monstercity.server.game.base.AllParam;
import com.motu.monstercity.server.game.base.CommonUtils;
import com.motu.monstercity.server.game.base.ProtoDataUtils;
import com.motu.monstercity.server.game.commondata.bag.*;
import com.motu.monstercity.server.game.commondata.maintask.MainTaskConstant;
import com.motu.monstercity.server.game.log.MotuLogManager;
import com.motu.monstercity.server.game.userdata.*;
import com.motu.vertx.module.utility.redis.RedisTools;
import com.motu.vertx.module.utility.toolset.Tool;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;

import java.util.ArrayList;
import java.util.List;

public class BuddyManager extends BuddyUserData {

    // 初始化 下发好友模块数据
    public static void loadInitBuddyUserData(PBUserData.Builder pbUserData, UserInfo userInfo) {
        long userId = userInfo.getId();
        //下发好友列表
        List<UserBuddy> list = BuddyManager.getUserBuddyList(userId);
        for (UserBuddy userBuddy : list) {
            ProtoDataUtils.updatePBUserData(pbUserData, userBuddy, true);
        }
        //下发黑名单列表
        List<UserBuddyBlack> userBuddyBlackList = getUserBuddyBlackList(userId);
        for (UserBuddyBlack userBuddyBlack : userBuddyBlackList) {
            ProtoDataUtils.updatePBUserData(pbUserData, userBuddyBlack,true);
        }
        //下发申请限制
        UserBuddyApplyInfo userBuddyApplyInfo = BuddyManager.getUserBuddyApplyInfo(userId);
        ProtoDataUtils.updatePBUserData(pbUserData, userBuddyApplyInfo,true);
    }

    /**
     * 检查好友模块红点
     * @param userInfo
     * @param builder
     * @return
     */
    public static boolean checkRedPoint(UserInfo userInfo,CsGameSystem.CSGameSystemRedPointResponse.Builder builder){
        boolean hasRedPoint = false;
        CsGameSystem.RedPoint_buddy.Builder redPoint = builder.getBuddyBuilder();
        long userId = userInfo.getId();

        //申请列表是否有红点
        List<UserBuddyApply> userBuddyApplyList = getUserBuddyApplyList(userId);
        if(userBuddyApplyList.size() > 0){
            hasRedPoint = true;
            redPoint.setApply(true);
        }
        //私聊列表是否有红点
        List<UserBuddy> list = getUserBuddyList(userId);
        for (UserBuddy userBuddy : list) {
            List<ChatBuddy> chatBuddyList = ChatManager.getChatBuddyList(userId,userBuddy.getBuddyId());
            for (ChatBuddy chatBuddy : chatBuddyList) {
                if (chatBuddy.getReceiveId() == userId && chatBuddy.getIsRead() == ChatManager.CHAT_IS_READ_NO) {
                    hasRedPoint = true;
                    redPoint.setChat(true);
                    break;
                }
            }
        }

        return hasRedPoint;

    }

    /**
     *
     * @param userId
     * @param buddyId
     * @return
     */
    public static PBUserBuddy.Builder takeBuddyBuilder(Common.PBUserBuddy.Builder pbUserBudddy,long userId,long buddyId) {
        if (pbUserBudddy == null) {
            pbUserBudddy = Common.PBUserBuddy.newBuilder();
        }
        UserShow userShow = GameUser.getUserShow(buddyId);
        pbUserBudddy.setUserShow(userShow.takeInitBuilder());
        UserInfo userInfo = GameUser.getUserInfo(buddyId);
        if (userInfo != null) {
            boolean isConnect = GameUser.isConnect(buddyId);
            if (isConnect) {
                pbUserBudddy.setLoginTime(0);// 在线返回0
            } else {
                pbUserBudddy.setLoginTime(userInfo.getLoginTime());
                UserExtInfo userExtInfo = GameUser.getUserExtInfo(buddyId);
                pbUserBudddy.setOffLineTime(userExtInfo.getOfflineTime());
            }
        }
        if (userId != 0) {
            //判断是否私聊有红点
            List<ChatBuddy> chatBuddyList = ChatManager.getChatBuddyList(userId,buddyId);
            for (ChatBuddy chatBuddy : chatBuddyList) {
                if (chatBuddy.getReceiveId() == userId && chatBuddy.getIsRead() == ChatManager.CHAT_IS_READ_NO) {
                    //如果私聊消息接收人是自己,并且消息未读,返回红点
                    pbUserBudddy.setIsChatRead(true);
                    break;
                }
            }
        }
        return pbUserBudddy;
    }

    public static boolean isBuddyNumMax(long uid) {
        long count = BuddyManager.getUserBuddyCount(uid);
        return count >= AllParam.BUDDY_ADD_MAX;
    }
    public static void addToRecommendList(long userId, JsonArray jsonArray, List<Long> buddyUserIdList, List<Long> blackUserIdList, List<Long> resultList) {
        for (int i = 0; i < jsonArray.size(); i++) {
            JsonObject jsonObject = jsonArray.getJsonObject(i);
            long uid = jsonObject.getLong("id");
            // 排重处理
            if (resultList.contains(uid)) {
                continue;
            }
            // 排除自己
            if (uid == userId) {
                continue;
            }
            // 排除没有起名
            String name = jsonObject.getString("user_name");
            if (name.isEmpty()) {
                continue;
            }
            // 判断是否已经是好友 或者 对方在黑名单之内
            if (buddyUserIdList.contains(uid) || blackUserIdList.contains(uid)) {
                continue;
            }
            // 判断对方好友是否已满
            if (BuddyManager.isBuddyNumMax(uid)) {
                continue;
            }
            // 判断是否已申请过
            UserBuddyApply userBuddyApply = BuddyManager.getUserBuddyApply(uid, userId);
            if (userBuddyApply != null) {
                continue;
            }
            resultList.add(uid);
            if (resultList.size() >= AllParam.BUDDY_RECOMMEND_LIST) {
                break;
            }
        }
    }
}
